package com.sugar.reflection;

import java.lang.reflect.Field;

/**
 *
 * 反射取值改值
 *
 * @author felix
 * @date 2025/2/10 14:43
 *
 */
public class ReflectDemo4 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
        Student s = new Student("zhangsan",23,"广州");
        Student ss = new Student("lisi",24,"北京");

        System.out.println(s);

        Class<?> clazz = Class.forName("com.sugar.reflection.Student");
//        Field field = clazz.getField("name");
        Field field = clazz.getDeclaredField("name");
        field.setAccessible(true);
        field.set(s,"wangwu");

        String res = (String) field.get(s);
        System.out.println(res);
        System.out.println(s);
    }
}
